package com.aistar.service.user.impl;

import com.aistar.mapper.EmployeeMapper;
import com.aistar.mapper.EmployerMapper;
import com.aistar.mapper.UserMapper;
import com.aistar.model.entity.Employee;
import com.aistar.model.entity.Employer;
import com.aistar.model.entity.User;
import com.aistar.service.user.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName: LoginServiceImpl
 * @author: LiuWenXiu
 * @date: 2021/10/16 10:04
 * @Description: TODO...本类用来演示:
 */
@Service
@Transactional
public class LoginServiceImpl implements ILoginService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BCryptPasswordEncoder encoder;

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private EmployerMapper employerMapper;

    @Override
    public User judgeLogin(String username, String password) {
        // 首先根据用户名获取用户实体
        User user = userMapper.findUserByUsername(username);
        // 判断是否能查询出用户
        if (user == null)
            return null;
        // 到这表示是有用户的,然后需要进行密码校验
        String codes = user.getPassword();
        if (encoder.matches(password, codes)) {
            // 验证成功,返回user
            return user;
        } else {
            // 验证失败,返回空
            return null;
        }
    }

    @Override
    public Integer sendEmailCode(String email) {
        // 首先判断该邮箱是否存在数据库中
        User user = userMapper.findByEmail(email);
        if (null == user)
            return -1;

        // 这一步表示该邮箱是存在在数据库中的,进行邮件发送功能
        SimpleMailMessage msg = new SimpleMailMessage();
        msg.setFrom("2294014200@qq.com");
        msg.setSubject("轻聘-简单生活,轻松应聘");
        // 获取验证码
        int num = (int) (Math.random() * 90000 + 10000);
        // 把验证码放到redis中并设置过期时间为五分钟
        ValueOperations vop = redisTemplate.opsForValue();
        vop.set(email, String.valueOf(num), 5, TimeUnit.MINUTES);

        // 发送验证码
        msg.setText("验证码:" + num + "\n请在五分钟内完成验证");
        msg.setTo(email);
        // 真正的发送
        mailSender.send(msg);
        return 1;
    }

    @Override
    public Integer judgeLoginByEmail(String email, String code) {
        // 首先验证邮箱是否存在在数据库中
        User user = userMapper.findByEmail(email);
        System.out.println(user);
        if (null == user)
            return -1;
        // 到这进行匹配
        // 获取redis中的验证码
        ValueOperations vop = redisTemplate.opsForValue();
        String realCode = (String) vop.get(email);
        // 表示验证码已过期
        if (null == realCode || realCode.length() == 0)
            return -2;
        // 表示验证成功
        if (realCode.equals(code)) {
            // 验证成功
            // 如果是求职者返回5
            // 如果是招聘者返回10
            if (user.getUserType().equals("求职者")) {
                System.out.println("求职者");
                return 5;
            } else {
                System.out.println("招聘者");
                return 10;
            }
        }
        return 0;
    }

    @Override
    public Employer loadEmployerByUserId(Integer userId) {
        return employerMapper.findByUserId(userId);
    }

    @Override
    public Employee loadEmployeeByUserId(Integer userId) {
        return employeeMapper.findNameByUserId(userId);
    }


}
